﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using HaiHui.Business;
using System.Windows.Interop;

namespace HaiHui
{
    /// <summary>
    /// Interaction logic for AlbumEditForm.xaml
    /// </summary>
    public partial class AlbumEditForm : Window
    {
        private Album m_Album;

        private List<Country> m_Countries;
        private List<City> m_Cities;

        public AlbumEditForm(Album album)
        {
            m_Album = album;
            InitializeComponent();
            m_Countries = HttpUtils.GetCountries();
            cmbTara.ItemsSource = m_Countries;

            if (m_Album == null)
                lblAlbumName.Content = "Adauga un album";
            else
            {
                lblAlbumName.Content = m_Album.Name;
                txtAlbumName.Text = m_Album.Name;
                txtDescriere.Text = m_Album.Descriere;
                txtCuvinteCheie.Text = m_Album.CuvinteCheie;
                chkAcceptaComentarii.IsChecked = m_Album.AcceptaComentarii;
                chkPublica.IsChecked = m_Album.Public;

                foreach (Country c in m_Countries)
                {
                    if (c.Name.Equals(m_Album.Tara))
                    {
                        cmbTara.SelectedItem = c;
                        break;
                    }
                }

                foreach (City c in m_Cities)
                {
                    if (c.Name.Equals(m_Album.Localitate))
                    {
                        cmbLocalitate.SelectedValue = c.Id;
                    }
                }

            }
        }

        bool ModifyStyle(IntPtr handle, int Remove, int Add)
        {
            const int GWL_STYLE = -16;
            int Style = PInvokes.GetWindowLong(handle, GWL_STYLE);
            int NewStyle = (Style & ~Remove) | Add;

            if (Style == NewStyle)
                return false;

            PInvokes.SetWindowLong(handle, GWL_STYLE, NewStyle);
            return true;
        }

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            Save();
            this.DialogResult = true;
            this.Close();
        }

        private void Save()
        {
            //throw new NotImplementedException();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            
        }

        private void cmbTara_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (cmbTara.SelectedValue != null)
            {
                int idCountry = (int)cmbTara.SelectedValue;
                m_Cities = HttpUtils.GetCities(idCountry);
                cmbLocalitate.ItemsSource = m_Cities;
            }
        }

        private void Window_SourceInitialized(object sender, EventArgs e)
        {
            IntPtr handle = (new WindowInteropHelper(this)).Handle;
            ModifyStyle(handle, 0x00020000, 0);
        }
    }
}
